﻿using System;
using System.Windows.Forms;
using Bule.Malbilden.Domain;
using System.Globalization;
using Bule.Malbilden.Util;

namespace Bule.Malbilden.Forms
{
    public partial class WeightPortionsInputbox : Form
    {
        private EatenAmount amount;
        private bool isChangeFromUserInput;

        public WeightPortionsInputbox(string portionName, double portionSize)
            : this(portionName, portionSize, 0) { }

        public WeightPortionsInputbox(string portionName, double portionSize, double defaultWeight)
        {
            InitializeComponent();

            amount = new EatenAmount(portionSize);

            if (defaultWeight > 0)
                amount.Weight = defaultWeight;
            else
                amount.Portions = 1;

            CheckParameters(portionSize);
            InitializeForm(portionName, portionSize);
        }

        private void CheckParameters(double portionSize)
        {
            if (portionSize <= 0)
            {
                MessageBox.Show("Portionsstorleken måste vara > 0 gram.", "Fel", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
        }

        private void InitializeForm(string portionName, double portionSize)
        {
            portionLabel.Text = "Antal " + portionName + " à " + Convert.ToString(portionSize) + " gram:";

            portionsTextbox.Text = amount.Portions.ToString(); ;
            weightTextbox.Text = amount.Weight.ToString();
        }

        private void textbox_KeyPress(object sender, KeyPressEventArgs e)
        {
            isChangeFromUserInput = true;
            e.Handled = !InputFilter.GivesValidNumeric(e.KeyChar, ((TextBox)sender).Text);
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void weightTextbox_TextChanged(object sender, EventArgs e)
        {
            if (isChangeFromUserInput)
            {
                try
                {
                    double newWeight = Convert.ToDouble(weightTextbox.Text, CultureInfo.CurrentCulture);
                    amount.Weight = newWeight;

                    isChangeFromUserInput = false;
                    portionsTextbox.Text = (Math.Round(amount.Portions, 2)).ToString();

                    okButton.Enabled = true;
                }
                catch (FormatException)
                {
                    okButton.Enabled = false;
                }
            }
        }

        private void portionsTextbox_TextChanged(object sender, EventArgs e)
        {
            if (isChangeFromUserInput)
            {
                try
                {
                    double newPortions = Convert.ToDouble(portionsTextbox.Text, CultureInfo.CurrentCulture);

                    amount.Portions = newPortions;

                    isChangeFromUserInput = false;
                    weightTextbox.Text = (Math.Round(amount.Weight, 2)).ToString();

                    okButton.Enabled = true;
                }
                catch (FormatException)
                {
                    okButton.Enabled = false;
                }
            }
        }

        public double Weight
        {
            get { return amount.Weight; }
        }

        public double Portions
        {
            get { return amount.Portions; }
        }
    }
}